using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    class clsVAT_TU_PHU_TUNG
    {
        public clsVAT_TU_PHU_TUNG(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<VAT_TU_PHU_TUNG> DataTableToLstObj(DataTable dt)
        {
            VAT_TU_PHU_TUNG vtptDTO;
            List<VAT_TU_PHU_TUNG> lstVtptDTO = new List<VAT_TU_PHU_TUNG>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                vtptDTO = new VAT_TU_PHU_TUNG();
                try
                {
                    vtptDTO._maVTPT = dt.Rows[i]["MaVTPT"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._donViTinh = dt.Rows[i]["DonViTinh"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._bacTrongCayPhanCap = int.Parse(dt.Rows[i]["BacTrongCayPhanCap"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._maNhaSanXuat = dt.Rows[i]["MaNhaSanXuat"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._maNhomVTPT = dt.Rows[i]["MaNhomVTPT"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._maSach = dt.Rows[i]["MaSach"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._maVTPTCha = dt.Rows[i]["MaVTPTCha"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._moTa = dt.Rows[i]["MoTa"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._soThuTu = dt.Rows[i]["SoThuTu"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._tenVTPT = dt.Rows[i]["TenVTPT"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._tenNSX = dt.Rows[i]["TenNhaSanXuat"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    vtptDTO._tenNhomVTPT = dt.Rows[i]["TenNhomVTPT"].ToString();
                }
                catch (Exception ex)
                {
                }
                lstVtptDTO.Add(vtptDTO);
            }
            return lstVtptDTO;
        }
        public VAT_TU_PHU_TUNG DataTableToObj(DataTable dt)
        {
            VAT_TU_PHU_TUNG vtptDTO = new VAT_TU_PHU_TUNG();
            try
            {
                vtptDTO._maVTPT = dt.Rows[0]["MaVTPT"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._donViTinh = dt.Rows[0]["DonViTinh"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._bacTrongCayPhanCap = int.Parse(dt.Rows[0]["BacTrongCayPhanCap"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._maNhaSanXuat = dt.Rows[0]["MaNhaSanXuat"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._maNhomVTPT = dt.Rows[0]["MaNhomVTPT"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._maSach = dt.Rows[0]["MaSach"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._maVTPTCha = dt.Rows[0]["MaVTPTCha"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._moTa = dt.Rows[0]["MoTa"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._soThuTu = dt.Rows[0]["SoThuTu"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._tenVTPT = dt.Rows[0]["TenVTPT"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._tenNSX = dt.Rows[0]["TenNhaSanXuat"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                vtptDTO._tenNhomVTPT = dt.Rows[0]["TenNhomVTPT"].ToString();
            }
            catch (Exception ex)
            {
            }
            return vtptDTO;
        }

        public List<VAT_TU_PHU_TUNG> showHierarchyDefault()
        {
            DataTable dt = new DataTable();
            List<VAT_TU_PHU_TUNG> lstVtptDTO = new List<VAT_TU_PHU_TUNG>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_ShowHierarchyDefault";
                //dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                //lst = GetVYLUONG_KHOANLUONG(dt);
                dt = SqlHelper.ExecuteToDataTable(spName,CommandType.StoredProcedure);
                lstVtptDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstVtptDTO;
        }

        public List<VAT_TU_PHU_TUNG> showHierarchyChildNodeByClick(string id)
        {
            DataTable dt = new DataTable();
            List<VAT_TU_PHU_TUNG> lstVtptDTO = new List<VAT_TU_PHU_TUNG>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_ShowHierarchyChildNodeByClick";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaVTPT", id);
                //dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                //lst = GetVYLUONG_KHOANLUONG(dt);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstVtptDTO = DataTableToLstObj(dt);
            }

            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstVtptDTO;
        }
        public List<VAT_TU_PHU_TUNG> SearchByManyCriteriasVAT_TU_PHU_TUNG(string matau, string makho, string mavtpt, string tenvtpt, string mansx, string partnumber, string model, string drawingnumber)
        {
            DataTable dt = new DataTable();
            List<VAT_TU_PHU_TUNG> lstVtptDTO = new List<VAT_TU_PHU_TUNG>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SearchByManyCriteriasVAT_TU_PHU_TUNG";
                SqlParameter[] par = new SqlParameter[8];

                par[0] = new SqlParameter("@matau", matau == "" ? null : matau);
                par[1] = new SqlParameter("@makho", makho ==""?null:makho);
                par[2] = new SqlParameter("@mavtpt", mavtpt == "" ? null : mavtpt);
                par[3] = new SqlParameter("@tenvtpt", tenvtpt == "" ? null : tenvtpt);
                par[4] = new SqlParameter("@mansx", mansx == "" ? null : mansx);
                par[5] = new SqlParameter("@partnumber", partnumber == "" ? null : partnumber);
                par[6] = new SqlParameter("@model", model == "" ? null : model);
                par[7] = new SqlParameter("@drawingnumber", drawingnumber == "" ? null : drawingnumber);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstVtptDTO = DataTableToLstObj(dt);
            }

            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstVtptDTO;
        }

        public List<VAT_TU_PHU_TUNG> SelectVAT_TU_PHU_TUNGByThanhPhanVTPTCha(string maVTPTCha, string thanhPhan)
        {
            DataTable dt = new DataTable();
            List<VAT_TU_PHU_TUNG> lstVtptDTO = new List<VAT_TU_PHU_TUNG>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectVAT_TU_PHU_TUNGByThanhPhanVTPTCha";
                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@MaVTPT", maVTPTCha);
                par[1] = new SqlParameter("@ThanhPhan", thanhPhan);
                //dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                //lst = GetVYLUONG_KHOANLUONG(dt);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstVtptDTO = DataTableToLstObj(dt);
            }

            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstVtptDTO;
        }
        public VAT_TU_PHU_TUNG SelectVAT_TU_PHU_TUNG(string id)
        {
            DataTable dt = new DataTable();
            VAT_TU_PHU_TUNG vtptDTO = new VAT_TU_PHU_TUNG();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectVAT_TU_PHU_TUNG";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaVTPT", id);
                //dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                //lst = GetVYLUONG_KHOANLUONG(dt);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                vtptDTO = DataTableToObj(dt);
            }

            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return vtptDTO;
        }
    }
}
